package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.views.RichTextView;

import java.text.DecimalFormat;
import java.util.List;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class CompaignMoreAdapter extends BaseListAdapter<LinkedTreeMap> {

    public CompaignMoreAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
                holder.noData = (TextView) convertView.findViewById(R.id.noData);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_compaign_more_item, parent, false);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.time = (TextView) convertView.findViewById(R.id.time);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
            holder.noData.setText("本店暂无活动");
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            if (!(mDataList.get(position).get("image")+"").equals("null")){
                Glide.with(mContext).load(mDataList.get(position).get("image")).placeholder(R.mipmap.detail_wait).error(R.mipmap.detail_error).into(holder.image);
            }else{
                Glide.with(mContext).load("").placeholder(R.mipmap.detail_wait).error(R.mipmap.detail_error).into(holder.image);
            }
            holder.name.setText(mDataList.get(position).get("title")+"");
            holder.time.setText((mDataList.get(position).get("beg_time")+"").substring(0,(mDataList.get(position).get("beg_time")+"").length()-3) + " - " + (mDataList.get(position).get("end_time")+"").substring(0,(mDataList.get(position).get("end_time")+"").length()-3));

        }
        return convertView;
    }

    private static final class ViewHolder {
        ImageView image;
        TextView name;
        TextView time;
        TextView noData;
        LinearLayout noDataRootLayout;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

}
